﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KVB.Common;
using Customer_Profile.eCard.DBDao;

namespace Customer_Profile.eCard
{
    public partial class mmdealSearch : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Comm.BindDll(ref dropTerm, "TermType", true, "");
                Comm.BindDll(ref dropEntity, "Entity", true, "");
            }
            bindGrid();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {

        }

        private void bindGrid()
        {

            try
            {
                DataSet ds = DealDao.getMMDeals();
                if (ds != null && ds.Tables.Count > 0)
                {
                    GvData.DataSource = ds.Tables[0];
                    GvData.DataBind();
                }
                else
                {
                    GvData.DataBind();
                }
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("bindGrid", ex.ToString());
            }
        }

        protected void GvData_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GvData.PageIndex = e.NewPageIndex;
            bindGrid();
        }

        protected void GvData_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //string Guid = GvData.DataKeys[e.Row.RowIndex].Value.ToString();
                //ImageButton ib = (ImageButton)e.Row.Cells[6].Controls[1];
                //ImageButton ibSend = (ImageButton)e.Row.Cells[7].Controls[1];
                //ib.Attributes.Add("onclick", "javascript:window.open('showResult.aspx?id=" + Guid + "');return false");
                //string SendStatus = e.Row.Cells[5].Text;
                //if (SendStatus.Equals("NO"))
                //{
                //    e.Row.Cells[5].Text = "<span style=color:red>" + SendStatus + "</span>";
                //}
                //if (e.Row.Cells[4].Text == "" || e.Row.Cells[4].Text.Equals("&nbsp;"))
                //{
                //    ibSend.Visible = false;
                //}
                //ibSend.Attributes.Add("Guid", Guid);
                //ibSend.Attributes.Add("ibid", e.Row.Cells[0].Text.ToString());

                //string strcardno = GvData.DataKeys[e.Row.DataItemIndex]["stateid_O2"].ToString(); 

                LinkButton lb = (LinkButton)e.Row.Cells[0].Controls[1];
                lb.Attributes.Add("onclick", "javascript:window.open('CardDetail.aspx?cardno=" + lb.Text + "');return false");

                //汇款
                ImageButton ibTransfer = (ImageButton)e.Row.Cells[10].Controls[1];
                string dealno = ibTransfer.CommandArgument.ToString();
                ibTransfer.Attributes.Add("onclick", "javascript:window.open('transferSearchList.aspx?CARDNO=" + lb.Text + "', '', 'toolbar=no,height=450,width=1024');return false");

                //卡帐
                ImageButton ibSend = (ImageButton)e.Row.Cells[11].Controls[1];
                //string dealno = ibSend.CommandArgument.ToString();
                ibSend.Attributes.Add("onclick", "javascript:window.open('cflowSearchList.aspx?CARDNO=" + lb.Text + "&dealno=" + dealno + "', '', 'toolbar=no,height=450,width=1024');return false");

                //退款
                ImageButton ibRefund = (ImageButton)e.Row.Cells[12].Controls[1];    
                //string dealno = ibRefund.CommandArgument.ToString();
                ibRefund.Attributes.Add("onclick", "javascript:window.open('refund.aspx?dealno=" + dealno + "', '', 'toolbar=no,height=450,width=1024');return false");

                //续存
                ImageButton ibReNew = (ImageButton)e.Row.Cells[14].Controls[1];
                //string dealno = ibRefund.CommandArgument.ToString();
                ibReNew.Attributes.Add("onclick", "javascript:window.open('reNew.aspx?dealno=" + dealno + "');return false");

                //结算
                ImageButton ibSettle = (ImageButton)e.Row.Cells[15].Controls[1];    
                //string dealno = ibRefund.CommandArgument.ToString();
                ibSettle.Attributes.Add("onclick", "javascript:window.open('dealSettle.aspx?dealno=" + dealno  + "', '', 'toolbar=no,height=450,width=1024');return false");
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("eCard_input.aspx", true);
        }

        //失效
        protected void imgValid_Click(object sender, ImageClickEventArgs e)
        {
            string dealno = ((ImageButton)sender).CommandArgument.ToString();

            bool bok = DealDao.Valid(dealno);
            if (bok)
                Response.Write("<script language=javascript>alert('success');</script>");
            else
                Response.Write("<script language=javascript>alert('error');</script>");
        }

        //续存
        protected void imgAgain_Click(object sender, ImageClickEventArgs e)
        {
            //string cardno = ((ImageButton)sender).Attributes["cardno"].ToString();
            string dealno = ((ImageButton)sender).CommandArgument.ToString();
            Response.Redirect("reNew.aspx?dealno=" + dealno, true);
        }
    }
}